<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;

use App\Models\Classify;
use Illuminate\Http\Request;

class ClassifyController extends AdminController {

    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model']       = 'classify';
        $this->_response['_active']['_action']      = '';
        $this->_response['_title']                  = '小一农货-分类管理';
    }

    public function index(Request $request){
        $search=$request->all();
        $page = $request->get('page', 1);
        $this->_response['_active']['_action']      = 'list';

        $classifyModel = new Classify();
        $data = $classifyModel->getData($search, $page, 20);

        $this->_response['classify'] = $data;

        $this->_response['classify']['pageData']['page'] = $page;
        $this->_response['classify']['pageData']['pageHtml'] = self::getPageHtml($page , $this->_response['classify']['pageData']['lastPage'] , '/admin/classify?');

        return view('admin.classify.list', $this->_response);
    }
//添加
    public function add(){
        $this->_response['_active']['_action']      = 'add';
        return view('admin.classify.add', $this->_response);
    }

    public function doAdd(\App\Http\Requests\Classify $request){
        $data = $request->all();

        $saveData = [
            'classname' => $data['classname'],
            'sort' => $data['sort'],
            'create_at' => date('Y-m-d H:i:s', time())
        ];

        $classifyModel = new Classify();
        if($classifyModel->saveData($saveData)){
            return response()->json(['code' => 0, 'msg' => '添加成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '添加失败']);
        }
    }

    //修改页面
    public function edit($id){
        $this->_response['_active']['_action']      = 'edit';
        $this->_response['classify'] = Classify::where('id', $id)->first();
        //return $this->_response;
        return view('admin.classify.edit', $this->_response);
    }

    public function doEdit(\App\Http\Requests\Classify $request){
        $data = $request->all();

        $saveEdit = [
            'classname' => $data['classname'],
            'sort' => $data['sort'],
            'updated_at' => date('Y-m-d H:i:s', time())
        ];

        $classifyModel = new Classify();
        if($classifyModel->saveEdit($saveEdit,$data['id'])){
            return response()->json(['code' => 0, 'msg' => '修改成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }
    //删除
    public function delete($id){
        $Del =[
            'deleted_at' => date('Y-m-d H:i:s', time())
        ];
        $classifyModel = new Classify();
        if($classifyModel->Del($Del,$id)){
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }


}